home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / answers / solar5.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.6 KB  |  219 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* solar5.c - a simple solar system.
  19.  *
  20.  *    F1 key            - print help information
  21.  *    SPACE Key        - toggle between solid/wireframe models
  22.  *    Escape Key        - exit program
  23.  */
  24. #include <GL/gl.h>
  25. #include <GL/glu.h>
  26. #include <GL/glut.h>
  27.  
  28. #include <stdio.h>
  29. #include <math.h>
  30.  
  31. /* Function Prototypes */
  32.  
  33. GLvoid initgfx( GLvoid );
  34. GLvoid keyboard( GLubyte, GLint, GLint );
  35. GLvoid specialkeys( GLint, GLint, GLint );
  36. GLvoid drawScene( GLvoid );
  37.  
  38. void checkError( char * );
  39. void printHelp( char * );
  40.  
  41. /* Global Variables */
  42.  
  43. static char *progname; 
  44.  
  45. static GLboolean filledFlag = GL_TRUE;
  46.  
  47. /* Global Definitions */
  48.  
  49. #define KEY_ESC    27    /* ascii value for the escape key */
  50.  
  51. void
  52. main( int argc, char *argv[] )
  53. {
  54.     GLsizei width, height;
  55.  
  56.     glutInit( &argc, argv );
  57.  
  58.     /* create a window that is 1/4 the size of the screen,
  59.      * and position it in the middle of the screen.
  60.      */
  61.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  62.     height = glutGet( GLUT_SCREEN_HEIGHT );
  63.     glutInitWindowPosition( width / 4, height / 4 );
  64.     glutInitWindowSize( width / 2, height / 2 );
  65.     glutInitDisplayMode( GLUT_RGBA );
  66.     glutCreateWindow( argv[0] );
  67.  
  68.     initgfx();
  69.  
  70.     glutKeyboardFunc( keyboard );
  71.     glutSpecialFunc( specialkeys );
  72.     glutDisplayFunc( drawScene ); 
  73.  
  74.     progname = argv[0];
  75.  
  76.     printHelp( progname );
  77.  
  78.     glutMainLoop();
  79. }
  80.  
  81. void
  82. printHelp( char *progname )
  83. {
  84.     fprintf(stdout, 
  85.         "\n%s - model some objects\n\n"
  86.         "F1 key        - print help information\n"
  87.         "SPACE key    - toggle between solid/wireframe mode\n"
  88.         "Escape Key    - exit the program\n\n",
  89.         progname);
  90. }
  91.  
  92. GLvoid
  93. initgfx( GLvoid )
  94. {
  95.     /* set clear color to black */
  96.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  97.  
  98.     glMatrixMode( GL_PROJECTION );
  99.     gluPerspective( 45.0, 1.0, 1.0, 20.0 );
  100.     glMatrixMode( GL_MODELVIEW );
  101. }
  102.  
  103. void 
  104. checkError( char *label )
  105. {
  106.     GLenum error;
  107.     while ( (error = glGetError()) != GL_NO_ERROR )
  108.         printf( "%s: %s\n", label, gluErrorString(error) );
  109. }
  110.  
  111. GLvoid 
  112. keyboard( GLubyte key, GLint x, GLint y )
  113. {
  114.     switch (key) {
  115.     case ' ':    /* toggle fill mode */
  116.         filledFlag = !filledFlag;
  117.         glutPostRedisplay();
  118.         break;
  119.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  120.         exit(0);
  121.     }
  122. }
  123.  
  124. GLvoid 
  125. specialkeys( GLint key, GLint x, GLint y )
  126. {
  127.     switch (key) {
  128.     case GLUT_KEY_F1:    /* Function key #1 */
  129.         /* print help information */
  130.         printHelp( progname );
  131.         break;
  132.     }
  133. }
  134.  
  135. GLvoid
  136. drawScene( GLvoid )
  137. {
  138.     static GLfloat    year = 45.0, day = -90.0;
  139.  
  140.     static GLfloat  yellow[] = { 1.0, 1.0, 0.0, 1.0 };
  141.     static GLfloat  blue[] = { 0.0, 0.0, 1.0, 1.0 };
  142.     static GLfloat  gray[] = { 0.2, 0.2, 0.2, 1.0 };
  143.  
  144.         glClear( GL_COLOR_BUFFER_BIT );
  145.  
  146.     glPushMatrix();
  147.  
  148.         /* move into the viewing volume */
  149.         glTranslatef( 0.0, 0.0, -10.0 );
  150.  
  151.         /* draw sun */
  152.  
  153.         glPushMatrix();
  154.  
  155.             /* Give the sun a yellow glow */
  156.             glColor4fv( yellow );
  157.           
  158.             /* rotate on our own axis */
  159.             glRotatef( 90.0, 1.0, 0.0, 0.0 );
  160.             if (filledFlag)
  161.                 glutSolidSphere( 0.7, 15, 15 );
  162.             else
  163.                 glutWireSphere( 0.7, 15, 15 );
  164.  
  165.         glPopMatrix();
  166.  
  167.         glPushMatrix();
  168.  
  169.             /* draw earth */
  170.  
  171.             /* rotate to the right time of year */
  172.             glRotatef( year, 0.0, 1.0, 0.0 );
  173.  
  174.             /* translate out to our orbit about the sun */
  175.             glTranslatef( 3.5, 0.0, 0.0 );
  176.             glPushMatrix();
  177.  
  178.                 /* set color for the earth */
  179.                 glColor4fv( blue );
  180.  
  181.                 /* rotate on our own axis */
  182.                 glRotatef( 90.0, 1.0, 0.0, 0.0 );
  183.                 if (filledFlag)
  184.                     glutSolidSphere( 0.4, 15, 15 );
  185.                 else
  186.                     glutWireSphere( 0.4, 15, 15 );
  187.                 
  188.             glPopMatrix();
  189.  
  190.             /* draw moon */
  191.             glPushMatrix();
  192.  
  193.                 /* set color for the moon */
  194.                 glColor4fv( gray );
  195.  
  196.                 /* rotate to the right time of day */
  197.                 glRotatef( day, 0.0, 1.0, 0.0 );
  198.  
  199.                 /* translate out to our orbit about the earth */
  200.                 glTranslatef( 1.0, 0.0, 0.0 );
  201.  
  202.                 /* rotate on our axis */
  203.                 glRotatef( 90.0, 1.0, 0.0, 0.0 );
  204.                 if (filledFlag)
  205.                     glutSolidSphere( 0.2, 15, 15 );
  206.                 else
  207.                     glutWireSphere( 0.2, 15, 15 );
  208.  
  209.             glPopMatrix();
  210.  
  211.         glPopMatrix();
  212.  
  213.     glPopMatrix();
  214.  
  215.     checkError( "drawScene" );
  216.  
  217.     glFlush();
  218. }
  219.